<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "vip_item".
 *
 * @property string $name
 * @property int $type
 * @property string $description
 * @property string $rule_name
 * @property resource $data
 * @property int $created_at
 * @property int $updated_at
 *
 * @property VipAssignment[] $vipAssignments
 * @property VipRule $ruleName
 * @property VipItemChild[] $vipItemChildren
 * @property VipItemChild[] $vipItemChildren0
 * @property VipItem[] $children
 * @property VipItem[] $parents
 */
class VipItem extends \yii\db\ActiveRecord
{

    const TYPE_ITEM = 2;
    const TYPE_ROLE = 1;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'vip_item';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['name', 'type'], 'required'],
            [['type', 'created_at', 'updated_at'], 'integer'],
            [['description', 'data'], 'string'],
            [['name', 'rule_name'], 'string', 'max' => 64],
            [['name'], 'unique'],
            [
                ['rule_name'],
                'exist',
                'skipOnError'     => true,
                'targetClass'     => VipRule::className(),
                'targetAttribute' => ['rule_name' => 'name']
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'name'        => 'Name',
            'type'        => 'Type',
            'description' => 'Description',
            'rule_name'   => 'Rule Name',
            'data'        => 'Data',
            'created_at'  => 'Created At',
            'updated_at'  => 'Updated At',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getVipAssignments()
    {
        return $this->hasMany(VipAssignment::className(), ['item_name' => 'name']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getRuleName()
    {
        return $this->hasOne(VipRule::className(), ['name' => 'rule_name']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getVipItemChildren()
    {
        return $this->hasMany(VipItemChild::className(), ['parent' => 'name']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getVipItemChildren0()
    {
        return $this->hasMany(VipItemChild::className(), ['child' => 'name']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getChildren()
    {
        return $this->hasMany(VipItem::className(), ['name' => 'child'])->viaTable('vip_item_child',
            ['parent' => 'name']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getParents()
    {
        return $this->hasMany(VipItem::className(), ['name' => 'parent'])->viaTable('vip_item_child',
            ['child' => 'name']);
    }

    public static function addPermission($name, $description = null, $rule = null, $date = null)
    {
        $item       = new VipItem();
        $item->name = $name;
        $item->type = 2;
        if (isset($description)) {
            $item->description = $description;
        }
        if (isset($date)) {
            $item->data = $date;
        }
        if (isset($rule)) {
            $item->ruleName = $rule;
        }
        $item->save();
    }

    public function beforeSave($insert)
    {
        if ($insert) {
            $this->created_at = $this->updated_at = time();
        } else {
            $this->updated_at = time();
        }

        return parent::beforeSave($insert);
    }

}
